/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.geass.gui.searchGUI;


import Client.geass.clientController.searchController.CityLogic;
import Client.geass.clientController.searchController.CityLogicInterface;
import Client.geass.gui.travellerGUI.ComboBoxModel;
import Client.geass.gui.travellerGUI.TableModel;

import Shared.geass.dataPOJO.City;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Kite
 */
public class CitySearchPanel extends javax.swing.JPanel {
   JFrame parent ;
    /**
     * Creates new form CitySearchPanel
     */
    public CitySearchPanel(JFrame parent) {
        this.parent=parent;
        initComponents();
        setTable();
    }

    public void init() {
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        searchButton = new javax.swing.JButton();

        jComboBox1.setModel(cityBox);

        jTable1.setModel(cityTable);
        jScrollPane1.setViewportView(jTable1);

        searchButton.setText("搜一下");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField1)
                        .addGap(18, 18, 18)
                        .addComponent(searchButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchButton))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 539, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
private void setTable(){
    jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        jTable2MouseClicked(evt);
    }});

        
    }
            private void jTable2MouseClicked(MouseEvent evt) {
                int index= jTable1.getSelectedRow();System.out.println(""+(citylist==null));
                if((index!=-1)&&(citylist!=null)){
                    System.out.println("in");
                String str = citylist.get(index).getDescription();
                new desciptionDialog(str,parent).setVisible(true);}
                
        }
    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        // TODO add your handling code here:
        String searchkey = String.valueOf(jComboBox1.getSelectedItem());
        String searchCondition = this.jTextField1.getText();
        if (searchCondition.equals("")) {
            JOptionPane.showMessageDialog(this, "请输入一些文字：）");
        } else {
            if (searchkey.equals("季节")) {
                this.searchBySeason(searchCondition);
            } else if (searchkey.equals("城市名")) {
                this.searchByCityName(searchCondition);
            } else {
                JOptionPane.showMessageDialog(this, "好奇怪的搜索条件=.=");
            }

        }


    }//GEN-LAST:event_searchButtonActionPerformed

    private void searchBySeason(String searchCondition) {
       citylist = controller.SearchBySeason(searchCondition);
      //  citylist=  dbcity.searchCityBySeason(searchCondition);
        if (citylist == null) {
            System.out.println("没有找到符合的城市");
            return;
        }
      this.cityTable.setDataVector(TableModel.getInstance().cityTableVector(citylist), TableModel.CITY_COLUMN_NAMES);
       
    }

    private void searchByCityName(String searchCondition) {
       citylist = controller.SearchByName(searchCondition);
        //citylist =  dbcity.searchCityByName(searchCondition);
        if (citylist == null) {
            System.out.println("没有找到符合的城市");
            return;
        }
       this.cityTable.setDataVector(TableModel.getInstance().cityTableVector(citylist), TableModel.CITY_COLUMN_NAMES);
       
    }
    
    
    ArrayList<City> citylist = null;
    CityLogicInterface controller = new CityLogic();
   // private DBcity dbcity = new DBcity();
    private DefaultTableModel cityTable = new DefaultTableModel(null, TableModel.CITY_COLUMN_NAMES);
    private DefaultComboBoxModel cityBox = new DefaultComboBoxModel(ComboBoxModel.CITY_COLUMN_NAMES);
  
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JButton searchButton;
    // End of variables declaration//GEN-END:variables
}
